<?php

/**
* 系统设置
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class System  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	public function index(){
		
		if(IS_POST){
			$lower_cash = isset( $_POST["lower_cash"] ) ? $_POST["lower_cash"] : "";
			$cost_exchange_rate = isset( $_POST["cost_exchange_rate"] ) ? $_POST["cost_exchange_rate"] : "";
			$num_max = isset( $_POST["num_max"] ) ? $_POST["num_max"] : "";
			$num_min = isset( $_POST["num_min"] ) ? $_POST["num_min"] : "";
			$money_percent = isset( $_POST["money_percent"] ) ? $_POST["money_percent"] : "";
			$baocang_precent = isset( $_POST["baocang_precent"] ) ? $_POST["baocang_precent"] : "";
			$money_max = isset( $_POST["money_max"] ) ? $_POST["money_max"] : "";
			$total_max = isset( $_POST["total_max"] ) ? $_POST["total_max"] : "";
			$max_can_use = isset( $_POST["max_can_use"] ) ? $_POST["max_can_use"] : "";
			$try_times = isset( $_POST["try_times"] ) ? $_POST["try_times"] : "";
			$current_version = isset( $_POST["current_version"] ) ? $_POST["current_version"] : "";
			$trust_num = isset( $_POST["trust_num"] ) ? $_POST["trust_num"] : "";
			$system_switch = isset( $_POST["system_switch"] ) ? $_POST["system_switch"] : "";
			$system_switch_desc = isset( $_POST["system_switch_desc"] ) ? $_POST["system_switch_desc"] : "";
			$sell_am_start = isset( $_POST["sell_am_start"] ) ? $_POST["sell_am_start"] : "";
			$sell_am_end = isset( $_POST["sell_am_end"] ) ? $_POST["sell_am_end"] : "";
			$sell_pm_start = isset( $_POST["sell_pm_start"] ) ? $_POST["sell_pm_start"] : "";
			$sell_pm_end = isset( $_POST["sell_pm_end"] ) ? $_POST["sell_pm_end"] : "";
			$open_am_start = isset( $_POST["open_am_start"] ) ? $_POST["open_am_start"] : "";
			$open_am_end = isset( $_POST["open_am_end"] ) ? $_POST["open_am_end"] : "";
			$open_pm_start = isset( $_POST["open_pm_start"] ) ? $_POST["open_pm_start"] : "";
			$open_pm_end = isset( $_POST["open_pm_end"] ) ? $_POST["open_pm_end"] : "";
			$atm_am_start = isset( $_POST["atm_am_start"] ) ? $_POST["atm_am_start"] : "";
			$atm_pm_start = isset( $_POST["atm_pm_start"] ) ? $_POST["atm_pm_start"] : "";
			$atm_am_end = isset( $_POST["atm_am_end"] ) ? $_POST["atm_am_end"] : "";
			$atm_pm_end = isset( $_POST["atm_pm_end"] ) ? $_POST["atm_pm_end"] : "";
			$max_realname = isset( $_POST["max_realname"] ) ? $_POST["max_realname"] : "";
			$min_realname = isset( $_POST["min_realname"] ) ? $_POST["min_realname"] : "";
			$max_password = isset( $_POST["max_password"] ) ? $_POST["max_password"] : "";
			$min_password = isset( $_POST["min_password"] ) ? $_POST["min_password"] : "";
			$cookie_length = isset( $_POST["cookie_length"] ) ? $_POST["cookie_length"] : "";
			$user_regex = isset( $_POST["user_regex"] ) ? $_POST["user_regex"] : "";
			$user_prefix = isset( $_POST["user_prefix"] ) ? $_POST["user_prefix"] : "";
			$user_min = isset( $_POST["user_min"] ) ? $_POST["user_min"] : "";
			$user_max = isset( $_POST["user_max"] ) ? $_POST["user_max"] : "";
			$agent_prefix = isset( $_POST["agent_prefix"] ) ? $_POST["agent_prefix"] : "";
			$agent_min = isset( $_POST["agent_min"] ) ? $_POST["agent_min"] : "";
			$agent_max = isset( $_POST["agent_max"] ) ? $_POST["agent_max"] : "";
			$bull_cost = isset( $_POST["bull_cost"] ) ? $_POST["bull_cost"] : "";
			$sell_cost = isset( $_POST["sell_cost"] ) ? $_POST["sell_cost"] : "";
			$save_cost = isset( $_POST["save_cost"] ) ? $_POST["save_cost"] : "";
			$save_day = isset( $_POST["save_day"] ) ? $_POST["save_day"] : "";
			$sell_min_time = isset( $_POST["sell_min_time"] ) ? $_POST["sell_min_time"] : "";
			$rest_filter = isset( $_POST["rest_filter"] ) ? $_POST["rest_filter"] : "";
			$bull_range_limit = isset( $_POST["bull_range_limit"] ) ? $_POST["bull_range_limit"] : "";
			$cost_sell_limit = isset( $_POST["cost_sell_limit"] ) ? $_POST["cost_sell_limit"] : "";
			$turnover_min = isset( $_POST["turnover_min"] ) ? $_POST["turnover_min"] : "";
			$min_money = isset( $_POST["min_money"] ) ? $_POST["min_money"] : "";
			$stamp = isset( $_POST["stamp"] ) ? $_POST["stamp"] : "";
			$manage_cost = isset( $_POST["manage_cost"] ) ? $_POST["manage_cost"] : "";
			$r_sell_min_time = isset( $_POST["r_sell_min_time"] ) ? $_POST["r_sell_min_time"] : "";
			$bull_range_limit = isset( $_POST["bull_range_limit"] ) ? $_POST["bull_range_limit"] : "";
			$st_bull_range_limit = isset( $_POST["st_bull_range_limit"] ) ? $_POST["st_bull_range_limit"] : "";
			$rstock_sell_min_time = isset( $_POST["rstock_sell_min_time"] ) ? $_POST["rstock_sell_min_time"] : "";
			$buy_limit_interval = isset( $_POST["buy_limit_interval"] ) ? $_POST["buy_limit_interval"] : "";
			$sell_limit_interval = isset( $_POST["sell_limit_interval"] ) ? $_POST["sell_limit_interval"] : "";
			if( ! is_int($lower_cash + 0) ){
				ajax_return( 0,"保证金预警不是有效的整数格式！");
			}
			if( ! is_int($cost_exchange_rate + 0) ){
				ajax_return( 0,"人民币与股币兑换不是有效的整数格式！");
			}
			if( ! is_int($num_max + 0) ){
				ajax_return( 0,"最大买入股数不是有效的整数格式！");
			}
			if( ! is_int($num_min + 0) ){
				ajax_return( 0,"最小买入股数不是有效的整数格式！");
			}
			if( ! is_int($money_percent + 0) ){
				ajax_return( 0,"单股可用额度百分比不是有效的整数格式！");
			}
			if( ! is_int($baocang_precent + 0) ){
				ajax_return( 0,"会员爆仓百分比不是有效的整数格式！");
			}
			if( ! is_int($money_max + 0) ){
				ajax_return( 0,"单笔额度限额不是有效的整数格式！");
			}
			if( ! is_int($total_max + 0) ){
				ajax_return( 0,"单股持仓总额不是有效的整数格式！");
			}
			if( ! is_int($max_can_use + 0) ){
				ajax_return( 0,"最高可用额度限制不是有效的整数格式！");
			}
			if( ! is_int($try_times + 0) ){
				ajax_return( 0,"尝试登陆次数不是有效的整数格式！");
			}
			
			if( ! is_int($trust_num + 0) ){
				ajax_return( 0,"委托交易数量不是有效的整数格式！");
			}
			
			if( ! $system_switch_desc  ){
				ajax_return( 0,"统维护时页面显示维护公告 (100字符内)不能为空！");
			}
			if( ! strtotime($sell_am_start) ){
				ajax_return( 0,"上午平仓开始时间不是有效的时间格式！");
			}
			if( ! strtotime($sell_am_end) ){
				ajax_return( 0,"上午平仓结束时间不是有效的时间格式！");
			}
			if( ! strtotime($sell_pm_start) ){
				ajax_return( 0,"下午平仓开始时间不是有效的时间格式！");
			}
			if( ! strtotime($sell_pm_end ) ){
				ajax_return( 0,"下午平仓结束时间不是有效的时间格式！");
			}
			if( ! strtotime($open_am_start) ){
				ajax_return( 0,"上午开市时间不是有效的时间格式！");
			}
			if( ! strtotime($open_am_end) ){
				ajax_return( 0,"上午收市时间不是有效的时间格式！");
			}
			if( ! strtotime($open_pm_start) ){
				ajax_return( 0,"下午开市时间不是有效的时间格式！");
			}
			if( ! strtotime($open_pm_end) ){
				ajax_return( 0,"下午收市时间不是有效的时间格式！");
			}
			if( ! strtotime($atm_am_start) ){
				ajax_return( 0,"上午提款开始时间不是有效的时间格式！");
			}
			if( ! strtotime($atm_pm_start) ){
				ajax_return( 0,"下午提款开始时间不是有效的时间格式！");
			}
			if( ! strtotime($atm_am_end) ){
				ajax_return( 0,"上午提款结束时间不是有效的时间格式！");
			}
			if( ! strtotime($atm_pm_end) ){
				ajax_return( 0,"下午提款结束时间不是有效的时间格式！");
			}
			if( ! is_int($max_realname + 0) ){
				ajax_return( 0,"用户姓名最大长度不是有效的整数格式！");
			}
			if( ! is_int($min_realname + 0) ){
				ajax_return( 0,"用户姓名最小长度不是有效的整数格式！");
			}
			if( ! is_int($max_password + 0) ){
				ajax_return( 0,"密码最大长度不是有效的整数格式！");
			}
			if( ! is_int($min_password + 0) ){
				ajax_return( 0,"密码最小长度不是有效的整数格式！");
			}
			if( ! is_int($cookie_length + 0) ){
				ajax_return( 0,"Session时间不是有效的整数格式！");
			}
			if( ! $user_regex ){
				ajax_return( 0,"账号限制规则不能为空！");
			}
			//if( ! $current_version  ){
			//	ajax_return( 0,"系统当前版本号不能为空！");
			//}
			if( ! is_int($user_prefix + 0) ){
				ajax_return( 0,"继承上级账号的字符长度不是有效的整数格式！");
			}
			if( ! is_int($user_min + 0) ){
				ajax_return( 0,"用户账号最小长度不是有效的整数格式！");
			}
			if( ! is_int($user_max + 0) ){
				ajax_return( 0,"用户账号最大长度不是有效的整数格式！");
			}
			if( ! is_int($agent_prefix + 0) ){
				ajax_return( 0,"代理商账户账号不是有效的整数格式！");
			}
			if( ! is_int($agent_min + 0) ){
				ajax_return( 0,"代理商账号最小长度不是有效的整数格式！");
			}
			if( ! is_int($agent_max + 0) ){
				ajax_return( 0,"代理商账号最大长度不是有效的整数格式！");
			}
			if( ! is_float($bull_cost + 0 ) ){
				ajax_return( 0,"买入手续费不是有效的小数格式！");
			}
			if( ! is_float($sell_cost + 0 ) ){
				ajax_return( 0,"卖出手续费不是有效的小数格式！");
			}
			//if( ! is_float($save_cost + 0 ) ){
			//	ajax_return( 0,"留仓费不是有效的小数格式！");
			//}
			if( ! is_int($save_day + 0) ){
				ajax_return( 0,"最长留仓时间不是有效的整数格式！");
			}
			if( ! is_int($sell_min_time + 0) ){
				ajax_return( 0,"融资最小卖出时间不是有效的整数格式！");
			}
			if( ! is_int($rest_filter + 0) ){
				ajax_return( 0,"留仓节假日是否计算留仓费不是有效的整数格式！");
			}
			if( ! is_int($bull_range_limit + 0) ){
				ajax_return( 0,"上涨买入限制不是有效的整数格式！");
			}
			if( ! is_int($cost_sell_limit + 0) ){
				ajax_return( 0,"规定时间内卖出加收手续费不是有效的整数格式！");
			}
			if( ! is_int($turnover_min + 0) ){
				ajax_return( 0,"成交额限制不是有效的整数格式！");
			}
			if( ! is_int($min_money + 0) ){
				ajax_return( 0,"最低充值额度不是有效的整数格式！");
			}
			if( ! is_float($stamp + 0 ) ){
				ajax_return( 0,"印花税不是有效的小数格式！");
			}
			if( ! is_float($manage_cost + 0 ) ){
				ajax_return( 0,"管理费不是有效的小数格式！");
			}
			if( ! is_int($r_sell_min_time + 0) ){
				ajax_return( 0,"融券最小卖出时间不是有效的整数格式！");
			}
			if( ! is_int($bull_range_limit + 0) ){
				ajax_return( 0,"升跌买入限制不是有效的整数格式！");
			}
			if( ! is_int($st_bull_range_limit + 0) ){
				ajax_return( 0,"ST股升跌买入限制不是有效的整数格式！");
			}
			if( ! is_int($rstock_sell_min_time + 0) ){
				ajax_return( 0,"融券最小卖出时间不是有效的整数格式！");
			}
			if( ! is_int($buy_limit_interval + 0) ){
				ajax_return( 0,"单次买入限制间隔不是有效的整数格式！");
			}
			if( ! is_int($sell_limit_interval + 0) ){
				ajax_return( 0,"单次卖出限制间隔不是有效的整数格式！");
			}
			$list[] = "update config_sys set `value` = '$lower_cash' where `name` = 'lower_cash'";
			$list[] = "update config_sys set `value` = '$cost_exchange_rate' where `name` = 'cost_exchange_rate'";
			$list[] = "update config_sys set `value` = '$num_max' where `name` = 'num_max'";
			$list[] = "update config_sys set `value` = '$num_min' where `name` = 'num_min'";
			$list[] = "update config_sys set `value` = '$money_percent' where `name` = 'money_percent'";
			$list[] = "update config_sys set `value` = '$baocang_precent' where `name` = 'baocang_precent'";
			$list[] = "update config_sys set `value` = '$money_max' where `name` = 'money_max'";
			$list[] = "update config_sys set `value` = '$total_max' where `name` = 'total_max'";
			$list[] = "update config_sys set `value` = '$max_can_use' where `name` = 'max_can_use'";
			$list[] = "update config_sys set `value` = '$try_times' where `name` = 'try_times'";
			//$list[] = "update config_sys set `value` = '$current_version' where `name` = 'current_version'";
			$list[] = "update config_sys set `value` = '$trust_num' where `name` = 'trust_num'";
			$list[] = "update config_sys set `value` = '$system_switch' where `name` = 'system_switch'";
			$list[] = "update config_sys set `value` = '$system_switch_desc' where `name` = 'system_switch_desc'";
			$list[] = "update config_sys set `value` = '$sell_am_start' where `name` = 'sell_am_start'";
			$list[] = "update config_sys set `value` = '$sell_am_end' where `name` = 'sell_am_end'";
			$list[] = "update config_sys set `value` = '$sell_pm_start' where `name` = 'sell_pm_start'";
			$list[] = "update config_sys set `value` = '$sell_pm_end' where `name` = 'sell_pm_end'";
			$list[] = "update config_sys set `value` = '$open_am_start' where `name` = 'open_am_start'";
			$list[] = "update config_sys set `value` = '$open_am_end' where `name` = 'open_am_end'";
			$list[] = "update config_sys set `value` = '$open_pm_start' where `name` = 'open_pm_start'";
			$list[] = "update config_sys set `value` = '$open_pm_end' where `name` = 'open_pm_end'";
			$list[] = "update config_sys set `value` = '$atm_am_start' where `name` = 'atm_am_start'";
			$list[] = "update config_sys set `value` = '$atm_pm_start' where `name` = 'atm_pm_start'";
			$list[] = "update config_sys set `value` = '$atm_am_end' where `name` = 'atm_am_end'";
			$list[] = "update config_sys set `value` = '$atm_pm_end' where `name` = 'atm_pm_end'";
			$list[] = "update config_sys set `value` = '$max_realname' where `name` = 'max_realname'";
			$list[] = "update config_sys set `value` = '$min_realname' where `name` = 'min_realname'";
			$list[] = "update config_sys set `value` = '$max_password' where `name` = 'max_password'";
			$list[] = "update config_sys set `value` = '$min_password' where `name` = 'min_password'";
			$list[] = "update config_sys set `value` = '$cookie_length' where `name` = 'cookie_length'";
			$list[] = "update config_sys set `value` = '$user_regex' where `name` = 'user_regex'";
			$list[] = "update config_sys set `value` = '$user_prefix' where `name` = 'user_prefix'";
			$list[] = "update config_sys set `value` = '$user_min' where `name` = 'user_min'";
			$list[] = "update config_sys set `value` = '$user_max' where `name` = 'user_max'";
			$list[] = "update config_sys set `value` = '$agent_prefix' where `name` = 'agent_prefix'";
			$list[] = "update config_sys set `value` = '$agent_min' where `name` = 'agent_min'";
			$list[] = "update config_sys set `value` = '$agent_max' where `name` = 'agent_max'";
			$list[] = "update config_sys set `value` = '$bull_cost' where `name` = 'bull_cost'";
			$list[] = "update config_sys set `value` = '$sell_cost' where `name` = 'sell_cost'";
			$list[] = "update config_sys set `value` = '$save_cost' where `name` = 'save_cost'";
			$list[] = "update config_sys set `value` = '$save_day' where `name` = 'save_day'";
			$list[] = "update config_sys set `value` = '$sell_min_time' where `name` = 'sell_min_time'";
			$list[] = "update config_sys set `value` = '$rest_filter' where `name` = 'rest_filter'";
			$list[] = "update config_sys set `value` = '$bull_range_limit' where `name` = 'bull_range_limit'";
			$list[] = "update config_sys set `value` = '$cost_sell_limit' where `name` = 'cost_sell_limit'";
			$list[] = "update config_sys set `value` = '$turnover_min' where `name` = 'turnover_min'";
			$list[] = "update config_sys set `value` = '$min_money' where `name` = 'min_money'";
			$list[] = "update config_sys set `value` = '$stamp' where `name` = 'stamp'";
			$list[] = "update config_sys set `value` = '$manage_cost' where `name` = 'manage_cost'";
			$list[] = "update config_sys set `value` = '$r_sell_min_time' where `name` = 'r_sell_min_time'";
			$list[] = "update config_sys set `value` = '$bull_range_limit' where `name` = 'bull_range_limit'";
			$list[] = "update config_sys set `value` = '$st_bull_range_limit' where `name` = 'st_bull_range_limit'";
			$list[] = "update config_sys set `value` = '$rstock_sell_min_time' where `name` = 'rstock_sell_min_time'";
			$list[] = "update config_sys set `value` = '$buy_limit_interval' where `name` = 'buy_limit_interval'";
			$list[] = "update config_sys set `value` = '$sell_limit_interval' where `name` = 'sell_limit_interval'";

			foreach($list as $sql){
				$this->db->query($sql);
			}

			ajax_return( 1, "更新成功" , "/System");
		} else{
			
			$sql = "select * from config_sys";
			
			$result = $this->db->query($sql)->result_array();
			$list_record = array();
			if($result){
				foreach($result as $key=>$val){
					$list_record[$val['name']] = $val["value"];
				}
			}
		
			$this->assign("result", $list_record);
			$this->display();
		}
	}
	
}
